#include "header/pyramid_entrance.h"

OSG_USING_NAMESPACE

double roompass_door_height = 90;
double roompass_door_width = 70;
double roompass_length = 300;

NodePtr createRoompass()
{
	Matrix m;
	
	
	// *********** Right ***********
	NodePtr right = makePlane(roompass_length, roompass_door_height, 50, 50);
	setName(right, "roompassRight");
	
	TransformPtr tright = Transform::create();
	beginEditCP(tright, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(roompass_length/2, roompass_door_height/2, roompass_door_width);
		m.setRotate(Quaternion(Vec3f(0,1,0), M_PI));
		tright->setMatrix(m);
	endEditCP(tright, Transform::MatrixFieldMask);
	
	NodePtr rightTrans = Node::create();
	beginEditCP(rightTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		rightTrans->setCore(tright);
		rightTrans->addChild(right);
	endEditCP(rightTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	// *********** Left ***********
	NodePtr left = makePlane(roompass_length, roompass_door_height, 50, 50);
	setName(left, "roompassLeft");
	
	TransformPtr tleft = Transform::create();
	beginEditCP(tleft, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(roompass_length/2, roompass_door_height/2, 0);
		//m.setRotate(Quaternion(Vec3f(0,1,0), M_PI));
		tleft->setMatrix(m);
	endEditCP(tleft, Transform::MatrixFieldMask);
	
	NodePtr leftTrans = Node::create();
	beginEditCP(leftTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		leftTrans->setCore(tleft);
		leftTrans->addChild(left);
	endEditCP(leftTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	// *********** Roof ***********
	NodePtr roof = makePlane(roompass_length, roompass_door_width, 50, 50);
	setName(roof, "roompassRoof");
	
	TransformPtr troof = Transform::create();
	beginEditCP(troof, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(roompass_length/2, roompass_door_height, roompass_door_width/2);
		m.setRotate(Quaternion(Vec3f(1,0,0), M_PI/2));
		troof->setMatrix(m);
	endEditCP(troof, Transform::MatrixFieldMask);
	
	NodePtr roofTrans = Node::create();
	beginEditCP(roofTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		roofTrans->setCore(troof);
		roofTrans->addChild(roof);
	endEditCP(roofTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	
	
	//Kammer Boden Texturen laden
	ImagePtr cryptWallImage = Image::create();
    cryptWallImage->read("materials/cryptWall.jpg");    
	    
	    
	//Aus dem Bild eine Textur machen
	TextureChunkPtr cryptWallTex = TextureChunk::create();
	beginEditCP(cryptWallTex);
		cryptWallTex->setImage(cryptWallImage);
		cryptWallTex->setMinFilter(GL_LINEAR);
		cryptWallTex->setMagFilter(GL_LINEAR);
		cryptWallTex->setWrapS(GL_REPEAT);
		cryptWallTex->setWrapT(GL_REPEAT);
		cryptWallTex->setEnvMode(GL_MODULATE);
	endEditCP(cryptWallTex);
	   
	SimpleMaterialPtr cryptWallMat = SimpleMaterial::create();
	beginEditCP(cryptWallMat); 
		//Materialfarbe bei seitlicher Ansicht (Brechungswinkel)
		cryptWallMat->setAmbient(Color3f(0.3, 0.3, 0.3));    
		//Bestimmt Farbe des Materials
		cryptWallMat->setDiffuse(Color3f(1.0, 1.0, 1.0));
		cryptWallMat->setSpecular(Color3f(0.1, 0.1, 0.1));
		cryptWallMat->addChunk(cryptWallTex);
	endEditCP(cryptWallMat);
	
	GeometryPtr cryptWallGeo = GeometryPtr::dcast(right->getCore());
	beginEditCP(cryptWallGeo);
		cryptWallGeo->setMaterial(cryptWallMat);
	endEditCP(cryptWallGeo);
	
	cryptWallGeo = GeometryPtr::dcast(left->getCore());
	beginEditCP(cryptWallGeo);
		cryptWallGeo->setMaterial(cryptWallMat);
	endEditCP(cryptWallGeo);
	
	cryptWallGeo = GeometryPtr::dcast(roof->getCore());
	beginEditCP(cryptWallGeo);
		cryptWallGeo->setMaterial(cryptWallMat);
	endEditCP(cryptWallGeo);
	
	
	
	
	// *********** Bottom ***********
	//NodePtr bottom = makeBox(roompass_length, 10, roompass_door_width, 10, 10, 10);
	NodePtr bottom = makePlane(roompass_length, roompass_door_width, 50, 50);
	setName(bottom, "roompassBottom");
	
	TransformPtr tbottom = Transform::create();
	beginEditCP(tbottom, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(roompass_length/2, 0, roompass_door_width/2);
		m.setRotate(Quaternion(Vec3f(1,0,0), -M_PI/2));
		tbottom->setMatrix(m);
	endEditCP(tbottom, Transform::MatrixFieldMask);
	
	NodePtr bottomTrans = Node::create();
	beginEditCP(bottomTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		bottomTrans->setCore(tbottom);
		bottomTrans->addChild(bottom);
	endEditCP(bottomTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	//Kammer Boden Texturen laden
	ImagePtr cryptBottomImage = Image::create();
    cryptBottomImage->read("materials/cryptBottom.jpg");	
	
	//Aus dem Bild eine Textur machen
	TextureChunkPtr cryptBottomTex = TextureChunk::create();
	beginEditCP(cryptBottomTex);
		cryptBottomTex->setImage(cryptBottomImage);
		cryptBottomTex->setMinFilter(GL_LINEAR);
		cryptBottomTex->setMagFilter(GL_LINEAR);
		cryptBottomTex->setWrapS(GL_REPEAT);
		cryptBottomTex->setWrapT(GL_REPEAT);
		cryptBottomTex->setScale(0.2);
		cryptBottomTex->setEnvMode(GL_MODULATE);
	endEditCP(cryptBottomTex);
	   
	SimpleMaterialPtr cryptBottomMat = SimpleMaterial::create();
	beginEditCP(cryptBottomMat); 
		//Materialfarbe bei seitlicher Ansicht (Brechungswinkel)
		cryptBottomMat->setAmbient(Color3f(0.3, 0.3, 0.3));    
		//Bestimmt Farbe des Materials
		cryptBottomMat->setDiffuse(Color3f(1.0, 1.0, 1.0));
		cryptBottomMat->setSpecular(Color3f(0.1, 0.1, 0.1));
		cryptBottomMat->addChunk(cryptBottomTex);
	endEditCP(cryptBottomMat);

	//Mittelpunkt des Texturtraegers heraus finden und die Textur darauf legen.
	GeometryPtr cryptBottomGeo = GeometryPtr::dcast(bottom->getCore());
	beginEditCP(cryptBottomGeo);
		cryptBottomGeo->setMaterial(cryptBottomMat);
	endEditCP(cryptBottomGeo);
	
	
	
	
	
	
	// Erstellen eines Knotens
	NodePtr holeRoompass = Node::create();
	beginEditCP(holeRoompass, Node::CoreFieldMask);
	{
		holeRoompass->setCore(Group::create());
		holeRoompass->addChild(rightTrans);
		holeRoompass->addChild(leftTrans);
		holeRoompass->addChild(bottomTrans);
		holeRoompass->addChild(roofTrans);
	}
	endEditCP  (holeRoompass, Node::CoreFieldMask);
	
	
	return holeRoompass;
}
